package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;

import com.itheima.pojo.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private DishService dishService;

    @Resource
    private ShoppingCartService shoppingShoppingCartService;

    @PostMapping("/add")
    public R addCart(@RequestBody ShoppingCart shoppingCart) {

        LambdaQueryWrapper<ShoppingCart> sw = new LambdaQueryWrapper<>();
        sw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        sw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        sw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart one = shoppingShoppingCartService.getOne(sw);
        if (one != null) {
            one.setNumber(one.getNumber()+1);
                shoppingShoppingCartService.updateById(one);
                return R.success(one);
        } else {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingShoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }


    }
//    接口二：查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart ::getUserId,BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingShoppingCartService.list(qw);

        return R.success(list);
    }
//    接口三：取消购物车
        @PostMapping("/sub")
    public R deleteUser(@RequestBody ShoppingCart shoppingCart){
            Long currentId = BaseContext.getCurrentId();
            LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
            qw.eq(shoppingCart.getUserId()!=null, ShoppingCart ::getDishId,shoppingCart.getDishId());
            qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart ::getSetmealId,shoppingCart.getSetmealId());
            qw.eq(ShoppingCart::getUserId,currentId);
            ShoppingCart one = shoppingShoppingCartService.getOne(qw);

            if (one ==null) {
                return R.error("未在购物车");
            }
            one.setNumber(one.getNumber() - 1);
            if (one.getNumber()<=0){
                return R.error("未在购物车");
            }

            shoppingShoppingCartService.updateById(one);
            return R.success(one);
        }



//    接口四：清空购物车
    @DeleteMapping("/clean")
        public R deleteclen (){

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart ::getUserId ,BaseContext.getCurrentId());

        shoppingShoppingCartService.remove(qw);

        return R.success("清空成功");
    }






}






































//        ArrayList<ShoppingCart> shoppingCarts = new ArrayList<>();
//
//        LambdaQueryWrapper<ShoppingCart> sw = new LambdaQueryWrapper<>();
//        sw.eq(ShoppingCart ::getUserId,shoppingCart.getUserId());
//
//
//
//
//        LambdaQueryWrapper<Dish> dw = new LambdaQueryWrapper<>();
//        dw.eq(shoppingCart.getDishId()!=null, Dish ::getId ,shoppingCart.getDishId());
//        List<Dish> list1 = dishService.list(dw);
//
//
//
//
//        List<Dish> list = dishService.list(dw);
//        String dishFlavor = shoppingCart.getDishFlavor();
//        ShoppingCart shoppingCart1 = new ShoppingCart();
//        BeanUtils.copyProperties(list, shoppingCart1);
//        shoppingCart1.setDishFlavor(dishFlavor);


